package com.zmn.oms.admin.controller.common;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.admin.controller.order.OrderBaseController;
import com.zmn.oms.business.interfaces.log.OrderLogBService;
import com.zmn.oms.business.interfaces.messageV1.text.CustomerMessageSendTextService;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.dto.OmsOperator;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.model.dto.messageV1.CustomerSendMessageDIO;
import com.zmn.oms.model.entity.log.OrderLog;
import com.zmn.oms.zmn.aop.OrderWorkLogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 类描述：发送短信
 *
 * @author liuyingying
 * @since 2022/9/22 19:28
 */
@Controller
@RequestMapping("/sendMessage")
public class SendMessageController extends OrderBaseController {

    @Resource
    private CustomerMessageSendTextService customerMessageSendTextService;
    @Resource
    private OrderLogBService orderLogBService;

    /**
     * 转单失败发送短信
     *
     * @param customerSendMessageDIO
     * @return
     */
    @RequestMapping("/orderTransferFail")
    @ResponseBody
    public ResultDTO sendOrderTransferFailMsg(CustomerSendMessageDIO customerSendMessageDIO) {
        customerMessageSendTextService.sendOrderTransferFailMessage(customerSendMessageDIO);
        return ResultDTO.success();
    }

    /**
     * 修改订单 - 话务确认报价失败
     *
     * @param customerSendMessageDIO
     * @return
     */
    @RequestMapping("/orderConfirmQuotedFail")
    @ResponseBody
    public ResultDTO sendOrderConfirmQuotedFailMessage(CustomerSendMessageDIO customerSendMessageDIO) {
        customerMessageSendTextService.sendOrderConfirmQuotedFailMessage(customerSendMessageDIO);
        // 发送日志
        OmsOperator operator = BeanMapper.map(customerSendMessageDIO, OmsOperator.class);
        OrderLog orderLog = BeanMapper.map(customerSendMessageDIO, OrderLog.class);
        orderLog.setType(OrderLogConsts.ORDER_LOG_TYPE_CUSTOMER_SEND_MESSAGE_CONFIRM_QUOTED);
        orderLog.setTypeName(OrderLogConsts.getOrderLogTypeName(orderLog.getType()));
        OrderWorkLogUtils.processOrderLogOperater(orderLog, operator);
        orderLog.setCreateTime(DateUtil.getNow());
        orderLogBService.save(orderLog);

        return ResultDTO.success();
    }
}
